import { CancelToken } from "axios";
import FormData from "form-data";
import { srRequest } from "../../utils/request.util";
import { SRUploadFileResp } from "./sr.api.response.model";
import { readScreenTokens } from "../../configs/env.config.load";

/**
 * sr 注册接口
 * @param data
 * @param cancelToken
 * @param option
 */
export const srRegisterApi = async (
  data: any,
  cancelToken?: CancelToken,
  option = {},
): Promise<{ token: string }> => {
  return srRequest({
    url: "/api/core/assets/register",
    method: "post",
    data,
    cancelToken,
    ...option,
  });
};

/**
 * sr 文件上传接口
 */
export const srUploadFileApi = async (
  screenId: string,
  data: FormData,
  cancelToken?: CancelToken,
  options = {},
): Promise<SRUploadFileResp> => {
  const token = readScreenTokens(screenId);
  if (!token) throw new Error(`screen token is not found ${screenId}`);
  return srRequest({
    url: "/api/storage/faultDescFiles/upload",
    method: "post",
    headers: {
      "device-key": token,
    },
    data,
    cancelToken,
    ...options,
  });
};
